package com.yanggu.mapreduce.nlineInputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.NLineInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2019/12/15 11:30
 * @description:
 */
public class NLineInputFormatDriver {

    public static void main(String[] args) throws Exception {

        args = new String[]{"input/NLineInputFormat", "output/NLineInputFormat"};

        //1. 获得job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //2. 关联Mapper、Reducer、Driver类
        job.setMapperClass(NLineInputFormatMapper.class);
        job.setReducerClass(NLineInputFormatReducer.class);
        job.setJarByClass(NLineInputFormatDriver.class);

        //3. 设置mapper输出的key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //4. 设置最终输出的key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //5. 设置InputFormat的类型
        job.setInputFormatClass(NLineInputFormat.class);

        //6. 设置每个切片中划分三行记录
        NLineInputFormat.setNumLinesPerSplit(job, 3);

        //7. 设置输入和输出的文件路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //8. 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }

}
